<template>
  <el-table :data="Hero" stripe>
    <el-table-column prop="ename" label="代码" width="100"></el-table-column>
    <el-table-column prop="name" label="英雄名称"></el-table-column>
    <el-table-column prop="title" label="称号"></el-table-column>
    <el-table-column label="职业"
    :filters="filters" :filter-method="(value, row) => row.heroType.map(v => v._id).includes(value)"
    >
      <template slot-scope="scope">
        <span>{{scope.row.heroType.map(v=>v.name).join(' / ')}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="avatar" label="头像">
      <template slot-scope="scope">
        <img :src="scope.row.avatar" style="height:3rem">
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="180">
      <template slot-scope="scope">
        <el-button type="text" size="small"
        @click="$router.push(`/heros/edit/${scope.row._id}`)">编辑</el-button>
        <el-button type="text" size="small"
        @click="remove(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data () {
    return {
      Hero: [],
      filters: []
    }
  },
  methods: {
    async getCat () {
      const { data: res } = await this.$http.get('/rest/categories')
      const cat = res.filter(item => item.parent && item.parent.name === '英雄列表')
      this.filters = cat.map(v => {
        return {
          text: v.name,
          value: v._id
        }
      })
    },
    async getHero () {
      const { data: res } = await this.$http.get('/rest/heros')
      this.Hero = res
    },
    remove (item) {
      this.$confirm(`是否删除英雄 "${item.name}" ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete(`/rest/heros/${item._id}`)
        this.$message.success('删除成功!')
        this.getHero()
      })
    }
  },
  created () {
    this.getCat()
    this.getHero()
  }
}
</script>

<style lang="less" scoped>
</style>
